package com.fw.application.controller.weChatOpen;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import me.chanjar.weixin.common.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

public class QRUtil {


    @Autowired
    private WxService wxService;

    public static String getToken() {

        String url = "https://api.weixin.qq.com/cgi-bin/token";
        HashMap<String, Object> pram = new HashMap<>();
        pram.put("grant_type", "client_credential");
        pram.put("appid", "wxb0ae8c9c2d12e64e");
        pram.put("secret", "75670de2f2bfac1e5aae6eee5c133491");
        String s = HttpUtil.get(url, pram);
        JSONObject entries = JSONUtil.parseObj(s);
        String accessToken = (String) entries.get("access_token");
        return accessToken;
    }

    public static Object getQr() throws FileNotFoundException {
        String token = getToken();
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token= " + token;
        HashMap<String, Object> pram = new HashMap<>();
        pram.put("path", "/pages/index/index");
        pram.put("scene","scene=123");
        pram.put("check_path",true);
        pram.put("env_version","release");

        String s1 = JSONUtil.toJsonStr(pram);
        String s = HttpUtil.post(url, s1);
        byte[] readBytes = IoUtil.readBytes(new ByteArrayInputStream(s.getBytes()), true);
        IoUtil.write(new FileOutputStream("C:\\Users\\guohaibin\\Desktop\\1.png"),true,readBytes);
//        JSONObject entries = JSONUtil.parseObj(s);
//        Object buffer = entries.get("buffer");
        return  s;
    }

    public static void main(String[] args) throws FileNotFoundException {
        Object qr = getQr();
        System.out.println(qr);



    }
}
